Skip to content

Conversation

@7ttp
Copy link
Contributor

@7ttp 7ttp commented Nov 26, 2025

summary

Fixes a type inference failure when calling schema().rpc().select() on functions that return SETOF from a different schema.

changes

  • Fall back to the function’s Returns type when SetofOptions.to references a table outside the current schema
  • Add a CrossSchemaError with a clear message when the return type cannot be inferred
  • Fix Relationships lookup to return null instead of throwing when a view is missing
  • Add a JSDoc example demonstrating the overrideTypes workaround for cross-schema functions

fixes

Closes #1845

@7ttp 7ttp requested review from a team as code owners November 26, 2025 18:07
@coveralls
Copy link

coveralls commented Nov 26, 2025

Coverage Status

coverage: 95.367% (+14.2%) from 81.176%
when pulling c255666 on 7ttp:fix-rpcfails
into af85057 on supabase:master.

fall back to returns type when table is in different schema

fixes supabase#1845
@mandarini mandarini self-assigned this Nov 27, 2025
@mandarini mandarini added the postgrest-js Related to the postgrest-js library. label Dec 3, 2025
Copy link
Contributor

@mandarini mandarini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @7ttp ! This is a good fix for the cross-schema type inference issue. A few things to address:

  • The error message mentions wrong API. The CrossSchemaError type says:
  Use .rpc<YourReturnType>() to specify the return type explicitly.

But rpc doesn't accept a return type parameter. The JSDoc example correctly shows .overrideTypes<>(). Please update the error message to match:

  type CrossSchemaError<TableRef extends string> = {
    error: true
  } & `Function returns SETOF from a different schema ('${TableRef}'). Use 
  .overrideTypes<YourReturnType>() to specify the return type explicitly.`
  • Can you please add a type test to prevent regressions? There's an existing pattern in packages/core/postgrest-js/test/override-types.test-d.ts. Something like:
  // Test cross-schema rpc falls back to Returns type
  {
    const result = await postgrest
      .schema('other_schema')
      .rpc('cross_schema_fn', {})

    expectType<{ id: string; user_id: string }[]>(result.data)
  }

Otherwise the approach looks good. Falling back to Returns type when the table isn't in the current schema is the right solution, and fixing the Relationships lookup to handle missing views is a nice catch.

@7ttp
Copy link
Contributor Author

7ttp commented Dec 3, 2025

Thanks for the PR @7ttp ! This is a good fix for the cross-schema type inference issue. A few things to address:

  • The error message mentions wrong API. The CrossSchemaError type says:
  Use .rpc<YourReturnType>() to specify the return type explicitly.

But rpc doesn't accept a return type parameter. The JSDoc example correctly shows .overrideTypes<>(). Please update the error message to match:

  type CrossSchemaError<TableRef extends string> = {
    error: true
  } & `Function returns SETOF from a different schema ('${TableRef}'). Use 
  .overrideTypes<YourReturnType>() to specify the return type explicitly.`
  • Can you please add a type test to prevent regressions? There's an existing pattern in packages/core/postgrest-js/test/override-types.test-d.ts. Something like:
  // Test cross-schema rpc falls back to Returns type
  {
    const result = await postgrest
      .schema('other_schema')
      .rpc('cross_schema_fn', {})

    expectType<{ id: string; user_id: string }[]>(result.data)
  }

Otherwise the approach looks good. Falling back to Returns type when the table isn't in the current schema is the right solution, and fixing the Relationships lookup to handle missing views is a nice catch.

Updated everything as requested,
Please rereview when available!

@7ttp 7ttp requested a review from mandarini December 3, 2025 10:25
Copy link
Contributor

@mandarini mandarini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Approving!

@mandarini mandarini merged commit ea490dd into supabase:master Dec 3, 2025
25 checks passed
@7ttp 7ttp deleted the fix-rpcfails branch December 3, 2025 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

postgrest-js Related to the postgrest-js library.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RPC fails with select when returning from another schema.

3 participants